<?php

require_once 'Ext/User/Verify/Abstract.php';

class Ext_User_Verify_Adapter_Ucenter extends Ext_User_Verify_Abstract
{
    const IDENTITY_NOT_FOUND = 'identityNotFound';
    const CREDENTIAL_INVALID = 'credentialInvalid';
    const UNCATEGORIZED      = 'uncategorized';
    protected $_messageTemplates = array(
        self::IDENTITY_NOT_FOUND => "A record with the supplied identity could not be found.",
        self::CREDENTIAL_INVALID => "Supplied credential is invalid.",
        self::UNCATEGORIZED      => "Uncategorized.",
    );
    
    public function verify()
    {
        $result = array(
            'code'        => Ext_User_Verify_Result::FAILURE,
            'messages'    => array(),
        );
        
        $userProfileRow = App_Model_DbTable_User_Profile::getInstance()->getRowByUserId($this->_data->userId);
        if(!$userProfileRow){
            $result['messages'][] = $this->_messager->createMessage(self::IDENTITY_NOT_FOUND);
            return new Ext_User_Verify_Result($result['code'], $result['messages']);
        }
        $username = $userProfileRow['username'];
        
        $ucenter = new Ext_Ucenter();
        $ucenterLoginResult = $ucenter->loginByUsername($username, $this->_data->password);
        list($uid, $username, $password, $email) = $ucenterLoginResult;         
        
        if($uid < 1){
            if($uid == -1) {
                $result['messages'][] = $this->_messager->createMessage(self::IDENTITY_NOT_FOUND);

            } elseif($uid == -2) {
                $result['messages'][] = $this->_messager->createMessage(self::CREDENTIAL_INVALID);
                
            } else {
                $result['messages'][] = $this->_messager->createMessage(self::UNCATEGORIZED);
            }
            
            return new Ext_User_Verify_Result($result['code'], $result['messages']);
        }


        $result['code'] = Ext_User_Verify_Result::SUCCESS;
        return new Ext_User_Verify_Result($result['code'], $result['messages']);
        
    }
}
